<?php
/**
 * Created by PhpStorm.
 * User: zlc
 * Date: 2018-07-19
 * Time: 上午 11:43
 * 统计管理
 */
namespace app\admin\controller;
use app\admin\model\MemberModel;
use think\Db;
class Count extends Base{
    public function memberCount(){

        $member =  new MemberModel();
        //查询今日统计
        $todydate = date('Y-m-d');
        $tody = " DATE_FORMAT(member_time,'%Y-%m-%d') = '".$todydate."'";
        $todycount = $member->getMemberCount($tody);
        //查询月统计
        $monthdate = date('Y-m');
        $month = " DATE_FORMAT(member_time,'%Y-%m') = '".$monthdate."'";
        $monthcount = $member->getMemberCount($month);
        //查询年统计
        $yeardate = date('Y');
        $year = " DATE_FORMAT(member_time,'%Y') = '".$yeardate."'";
        $yearcount = $member->getMemberCount($year);

        $this->assign('tcount',$todycount);
        $this->assign('mcount',$monthcount);
        $this->assign('ycount',$yearcount);
        return $this->fetch();
    }
    public function memberCountTb(){
        $param = input('param.');
        $arr = array();
        if(!empty($param['data'])) {
            $start = rtrim($param['data'][0], " ");
            $end =substr($param['data'][1],1);
            if ($param['type']) {
                if ($param['type'] == 1) {
                    //日
                    $map["DATE_FORMAT(member_time,'%Y-%m-%d')"] = ['>',$start];
                    $maps["DATE_FORMAT(member_time,'%Y-%m-%d')"] = ['<',$end];
                    $data = Db::name('member')->field("member_time,count(*) as count")->where($map)->where($maps)->group("DATE_FORMAT(member_time,'%Y-%m-%d')")->select();
                } elseif ($param['type'] == 2) {
                    //月
                    $map["DATE_FORMAT(member_time,'%Y-%m')"] = ['>',$start];
                    $maps["DATE_FORMAT(member_time,'%Y-%m')"] = ['<',$end];
                    $data = Db::name('member')->field("member_time,count(*) as count")->where($map)->where($maps)->group("DATE_FORMAT(member_time,'%Y-%m')")->select();
                } else {
                    //年
                    $map["DATE_FORMAT(member_time,'%Y')"] = ['>',$start];
                    $maps["DATE_FORMAT(member_time,'%Y')"] = ['<',$end];
                    $data = Db::name('member')->field("member_time,count(*) as count")->where($map)->where($maps)->group("DATE_FORMAT(member_time,'%Y')")->select();
                }
                for($i=0;$i<count($data);$i++){
                    if($param['type'] == 1){
                        $arr['x'][$i] = date('d',strtotime($data[$i]['member_time']));
                        $arr['y'][$i] = $data[$i]['count'];
                    }elseif($param['type'] == 2){
                        $arr['x'][$i] = date('m-d',strtotime($data[$i]['member_time']));
                        $arr['y'][$i] = $data[$i]['count'];
                    }else{
                        $arr['x'][$i] = date('Y',strtotime($data[$i]['member_time']));
                        $arr['y'][$i] = $data[$i]['count'];
                    }

                }

                ajaxReturn(1, '成功', $arr);
            }
        }else{
            if ($param['type']) {
                if ($param['type'] == 1) {
                    //日
                    $start =  date('Y-m-d',strtotime('-7 days'));
                    $end = date('Y-m-d',strtotime('+1 days'));
                    $map["DATE_FORMAT(member_time,'%Y-%m-%d')"] = ['>',$start];
                    $maps["DATE_FORMAT(member_time,'%Y-%m-%d')"] = ['<',$end];
                    $data = Db::name('member')->field("member_time,count(*) as count")->where($map)->where($maps)->group("DATE_FORMAT(member_time,'%Y-%m-%d')")->select();
                } elseif ($param['type'] == 2) {
                    //月
                    $start =  date('Y-m',strtotime('-1 month'));
                    $end = date('Y-m',strtotime('+1 month'));
                    $map["DATE_FORMAT(member_time,'%Y-%m')"] = ['>',$start];
                    $maps["DATE_FORMAT(member_time,'%Y-%m')"] = ['<',$end];
                    $data = Db::name('member')->field("member_time,count(*) as count")->where($map)->where($maps)->group("DATE_FORMAT(member_time,'%Y-%m')")->select();
                } else {
                    //年
                    $start =  date('Y',strtotime('-1 year'));
                    $end = date('Y',strtotime('+1 year'));
                    $map["DATE_FORMAT(member_time,'%Y')"] = ['>',$start];
                    $maps["DATE_FORMAT(member_time,'%Y')"] = ['<',$end];
                    $data = Db::name('member')->field("member_time,count(*) as count")->where($map)->where($maps)->group("DATE_FORMAT(member_time,'%Y')")->select();
                }
                for($i=0;$i<count($data);$i++){
                    if($param['type'] == 1){
                        $arr['x'][$i] = date('d',strtotime($data[$i]['member_time']));
                        $arr['y'][$i] = $data[$i]['count'];
                    }elseif($param['type'] == 2){
                        $arr['x'][$i] = date('m-d',strtotime($data[$i]['member_time']));
                        $arr['y'][$i] = $data[$i]['count'];
                    }else{
                        $arr['x'][$i] = date('Y',strtotime($data[$i]['member_time']));
                        $arr['y'][$i] = $data[$i]['count'];
                    }

                }
                ajaxReturn(1, '成功', $arr);
            }
        }
    }
    //违章删除
    public function illegalDel(){
        $illegal = new IllegalModel();
        $param = input('param.');
        if(!$param['id']){
            ajaxReturn(-1,'删除失败');
        }
        $map = " illegal_id = ".$param['id'];
        $res = $illegal->getIllegalDel($map);
        if($res === false){
            ajaxReturn(-1,'操作失败');
        }else{
            ajaxReturn(1,'操作成功');
        }
    }
    //违章订单导出
    public function illegalOut(){
        $param = input('param.');
        $where = [];
        if (!empty($param['phone'])) {
            $where['snake_member.member_mobile'] = ['like', '%' . $param['phone'] . '%'];
        }
        if (!empty($param['type'])) {
            $where['snake_illegal.illegal_status'] = $param['type'];
        }
        if (!empty($param['name'])) {
            $where['snake_member.member_name'] = ['like', '%' . $param['name'] . '%'];
        }
        if (!empty($param['order_sn'])) {
            $where['snake_illegal.illegal_order_sn'] = ['like', '%' . $param['order_sn'] . '%'];
        }
        if (!empty($param['car_number'])) {
            $where['snake_car.car_number'] = ['like', '%' . $param['car_number'] . '%'];
        }
        $join = "snake_member";
        $condition="snake_illegal.member_id = snake_member.member_id";
        $join1="snake_car";
        $condition1="snake_illegal.car_id = snake_car.car_id";
        $illegal =  new IllegalModel();
        $selectResult = $illegal->getIllegalOutByWhere($where,$join,$condition,$join1,$condition1);
        foreach($selectResult as $key=>$vo){
            $selectResult[$key]['illegal_isonpay'] = $illegal->getIllegalIsPay($vo['illegal_isonpay']);
            $selectResult[$key]['illegal_ispay'] = $illegal->getIllegalIsPayJf($vo['illegal_ispay']);
            $selectResult[$key]['illegal_status'] = $illegal->getIllegalStatus($vo['illegal_status']);
        }
        $Excel = new Excel();
        $filename = '违章订单';
        $title = array('会员名称','会员手机号','订单号','违章车辆','罚单详情','通知书号','违章时间','违法行为','违法地点','违法性质','是否缴费','是否支付','支付时间','缴费时间','缴费金额','订单状态','下单时间');
        $field = array('member_name','member_mobile','illegal_order_sn','car_number','illegal_ticket','illegal_no','illegal_time','illegal_action','illegal_place','illegal_type','illegal_ispay','illegal_isonpay','illegal_paytime','illegal_ispay_time','illegal_price','illegal_status','illegal_downtime');
        $Excel->out($filename,$filename,17,$title,$field,$selectResult);die;
    }
    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '删除' => [
                'auth' => 'order/illegaldel',
                'href' => "javascript:illegalDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}